package cn.zcscloud.mall.controller;

import cn.zcscloud.mall.common.PageRequest;
import cn.zcscloud.mall.common.R;
import cn.zcscloud.mall.mbg.model.UmsMenu;
import cn.zcscloud.mall.mbg.model.UmsResource;
import cn.zcscloud.mall.mbg.model.UmsRole;
import cn.zcscloud.mall.service.UmsRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
@Slf4j
public class UmsRoleController {

    @Autowired
    private UmsRoleService umsRoleService;

    /**
     * 给角色分配菜单
     *
     * @param menuIds 分配的菜单id
     * @param roleId  角色id
     * @return
     */
    @PostMapping("/allocMenu")
    public R<String> allocMenu(@RequestParam List<Integer> menuIds, Integer roleId) {
        umsRoleService.allocMenu(menuIds, roleId);
        return R.success(null);
    }

    /**
     * 给角色分配资源
     *
     * @param resourceIds
     * @param roleId
     * @return
     */
    @PostMapping("/allocResource")
    public R<String> allocResource(@RequestParam List<Integer> resourceIds, Integer roleId) {
        umsRoleService.allocResource(resourceIds, roleId);
        return R.success(null);
    }

    /**
     * 添加角色
     *
     * @param umsRole
     * @return
     */
    @PostMapping("/create")
    public R<String> create(@RequestBody UmsRole umsRole) {
        umsRoleService.save(umsRole);
        return R.success(null);
    }

    /**
     * 批量删除角色
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public R<String> delete(@RequestParam List<Integer> ids) {
        umsRoleService.deleteByIds(ids);
        return R.success(null);
    }

    /**
     * 根据角色名称分页获取角色列表
     *
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public R<PageRequest> list(String keyword, Integer pageNum, Integer pageSize) {
        //分页构造器
        IPage<UmsRole> iPage = new Page<>(pageNum, pageSize);
        //查询构造器
        LambdaQueryWrapper<UmsRole> lqw = new LambdaQueryWrapper<>();
        lqw.like(keyword != null, UmsRole::getName, keyword);
        lqw.orderByDesc(UmsRole::getSort);
        umsRoleService.page(iPage, lqw);
        PageRequest pr = PageRequest.init(iPage);
        return R.success(pr);
    }

    /**
     * 获取所有角色
     *
     * @return
     */
    @GetMapping("/listAll")
    public R<List<UmsRole>> listAll() {
        //查询构造器
        LambdaQueryWrapper<UmsRole> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UmsRole::getStatus,1).orderByDesc(UmsRole::getSort);
        List<UmsRole> list = umsRoleService.list(lqw);
        return R.success(list);
    }

    /**
     * 根据角色id获取角色相关菜单
     *
     * @param roleId
     * @return
     */
    @GetMapping("/listMenu/{roleId}")
    public R<List<UmsMenu>> listMenu(@PathVariable("roleId") Integer roleId) {
        log.info("=============================={}", roleId);
        List<UmsMenu> list = umsRoleService.listMenu(roleId);
        return R.success(list);
    }

    /**
     * 根据角色id获取角色相关资源
     *
     * @param roleId
     * @return
     */
    @GetMapping("/listResource/{roleId}")
    public R<List<UmsResource>> listResource(@PathVariable("roleId") Integer roleId) {
        List<UmsResource> list = umsRoleService.listResource(roleId);
        return R.success(list);
    }

    /**
     * 修改角色
     *
     * @param id
     * @param umsRole
     * @return
     */
    @PostMapping("/update/{id}")
    public R<String> update(@PathVariable("id") Integer id, @RequestBody UmsRole umsRole) {
        umsRole.setId(id.longValue());
        umsRoleService.updateById(umsRole);
        return R.success(null);
    }

    /**
     * 修改角色状态
     *
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/updateStatus/{id}")
    public R<String> updateStatus(@PathVariable("id") Integer id, Integer status) {
        //构造修改对象
        UpdateWrapper<UmsRole> uw = new UpdateWrapper<>();
        uw.lambda().eq(UmsRole::getId, id);
        uw.lambda().set(UmsRole::getStatus, status);
        umsRoleService.update(uw);
        return R.success(null);
    }
}
